package com.base.jdk8.lambda;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ReadFileContent {

    /**
     * 一次读取一行
     *
     * @return
     * @throws IOException
     */
    public static String processFile() throws IOException {
        try (BufferedReader br = new BufferedReader(new FileReader("./test.txt"))) {
            return br.readLine();
        }
    }

    /**
     * 一次读取两行
     *
     * @return
     * @throws IOException
     */
    public static String processFile2() throws IOException {
        try (BufferedReader br = new BufferedReader(new FileReader("./test.txt"))) {
            return br.readLine() + br.readLine();
        }
    }

    public static String generalProcessFile(FileProcessor fileProcessor) throws IOException {
        try (BufferedReader br = new BufferedReader(new FileReader("./test.txt"))) {
            return fileProcessor.readFile(br);
        }
    }

    public static void main(String[] args) throws IOException {
        generalProcessFile((BufferedReader::readLine));
    }
}

@FunctionalInterface
interface FileProcessor {
    String readFile(BufferedReader reader) throws IOException;
}
